package com.sq.partner.manager.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.IGatewayNocarBaseDao;
import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.dao.IGatewayEmailDao;
import com.sq.partner.manager.dao.IGatewayPolicyDao;
import com.sq.partner.manager.dao.IGatewaySMSDao;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewayEmail;
import com.sq.partner.manager.mybatis.model.GatewayNocarBase;
import com.sq.partner.manager.mybatis.model.GatewayPolicy;
import com.sq.partner.manager.mybatis.model.GatewaySMS;
import com.sq.partner.manager.service.IGatewayNocarPolicyListService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.excel.ExcelEntity;
import com.sq.partner.manager.util.excel.ExportExcelBuilderTool;

@Service
public class GatewayNocarPolicyListServiceImpl implements
		IGatewayNocarPolicyListService {

	@Resource
	ExportExcelBuilderTool exportExcelBuilderTool;

	@Resource
	private IGatewayPolicyDao policyDao;

	@Resource
	private IGatewayChannelInfoConfigDao channelInfoConfigDao;
    @Resource
    private IGatewayNocarBaseDao  gatewayNocarBaseDao;
    @Resource
    private IGatewayEmailDao gatewayEmailDao;
    
    @Resource
    private IGatewaySMSDao gatewaySMSDao;
	@Override
	public Page queryNocarPolicyPageByCriteria(Page page) throws Exception {
		return policyDao.queryNocarPolicyPageByCriteria(page);
	}

	@Override
	public List<Map<String, Object>> queryNocarPolicyListByCriteria(
			Map<String, Object> map) throws Exception {
		return policyDao.queryNocarPolicyListByCriteria(map);
	}
	
	@Override
	public void downloadNocarPolicyFile(
			List<Map<String, Object>> nocarPolicyList,
			HttpServletResponse response) throws Exception {

		ExcelEntity excel = new ExcelEntity();
		excel.setResults(nocarPolicyList);
		excel.setFileName("NocarPolicyInfo");
		excel.setTitles("保单号,投保人,投保单位,投保单位子机构,出单机构,投保方案,经办人,经办人电话,人数,保费,保单提交日期,起保日期,终保日期,单证号,员工编号,状态");
		excel.setTitleKeys("POLNO,TBR,CHANNEL_NAME,SUB_CHANNEL_NAME,DPT_CDE,PLAN_NAME,PURVEYOR_NAME,PURVEYOR_TEL,BBR_NUM,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,SPICAL1,STATE_ID");
		excel.setColumns("35,10,25,25,60,30,15,15,10,15,23,23,23,25,15,10");

		exportExcelBuilderTool.buildExcel(response,excel);	
	}

	@Override
	public List<Map> queryChannelListOfNocar(
			GatewayChannelInfo channelInfo,String flag) throws Exception {
		return channelInfoConfigDao.queryChannelInfoList(channelInfo,flag);
	}

	@Override
	public GatewayPolicy queryPolicyInfoByPolNo(String polNo) throws Exception {
		// TODO Auto-generated method stub
		return policyDao.queryNocarPolicyInfoByPolNo(polNo);
	}

	@Override
	public GatewayNocarBase queryNocarBaseByPolicyId(String policyId)
			throws Exception {
		return gatewayNocarBaseDao.queryNocarBaseByPolicyId(policyId);
	}

	@Override
	public Result addEmailReSend(String polNo, String email) throws Exception {
		/**查询已发送过的邮件*/
		List<GatewayEmail> emails=gatewayEmailDao.queryEmailByPolNo(polNo);
		if(emails==null||emails.size()==0){
			return Result.error("此单没有历史邮件发送记录");
		}
		GatewayEmail emailDto=emails.get(0);
		emailDto.setEmail(email);
		if(emailDto.getEmailId().contains("CF")){
			emailDto.setEmailId("CF"+(Integer.parseInt(emailDto.getEmailId().replace("CF", ""))+emails.size()));
		}else{
			emailDto.setEmailId("CF"+emailDto.getEmailId()+emails.size());
		}
		
		if(gatewayEmailDao.addGateWayEmail(emailDto)){
			return Result.success("发送成功");
		};
		return Result.error("发送失败");
	}

	@Override
	public Result addSMSReSend(String polNo, String phoneNo) throws Exception {
		SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format1=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		/**查询已发送过的短信*/
		List<GatewaySMS> emss=gatewaySMSDao.querySMSByPolNo(polNo);
		if(emss==null||emss.size()==0){
			return Result.error("此单没有历史短信发送记录");
		}
		GatewaySMS ems=emss.get(0);
		if(ems.getSmsId().contains("CF")){
			ems.setSmsId("CF"+(Integer.parseInt(ems.getSmsId().replace("CF", ""))+emss.size()));
		}else{
			ems.setSmsId("CF"+ems.getSmsId()+emss.size());
		}
		ems.setPhoneCode(phoneNo);
		ems.setSendTime(format.format(new Date()));
		ems.setId("CF"+format1.format(new Date())+"560709");
		if(gatewaySMSDao.addGateWaySMS(ems)){
			return Result.success("发送成功");
		};
		return Result.error("发送失败");
	}

	@Override
	public Map<String, Object> queryNocarPolicyAll(Map<String, String> map) throws Exception {
		return policyDao.queryNocarPolicyAll(map);
	}

	@Override
	public Map<String, String> queryOFDAndProofDownloadUrl(String policyId) throws Exception {
		// TODO Auto-generated method stub
		return policyDao.queryOFDAndProofDownloadUrl(policyId);
	}
}
